home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 2003 June / macformat-130.iso / pc / Shareware_Freeware (OS X) / Internet / soybo-1.0.1 / macosx_components / Sample Soyvices / Text to Speech / Text to Speech.php < prev   
Encoding:
PHP Script  |  2003-03-27  |  3.0 KB  |  101 lines

  1. <?php
  2.  
  3. /* Copyright (C) 2003 Adam Tow
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 2 of the License, or
  8.    (at your option) any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
  18.    
  19.  
  20. function RunWithParams($methodName, $params)
  21. {    
  22.     $response = array(
  23.                         "template" => "text_to_speech.tpl",
  24.                         "override" => $params["override"],
  25.                     );
  26.                         
  27.     if($methodName == "speak") {
  28.  
  29.         $text = $params["text_to_speak"];
  30.         
  31.                         
  32.         if($text) {
  33.             
  34.                 // Massage the text so AppleScript doesn't barf on it
  35.                 
  36.             $text = str_replace("\\'", "'\"'\"'", $text);
  37.             
  38.             //echo "Text to speak: $text<br />";
  39.             
  40.                 // Create a temp file to store the AIFF sound data
  41.             
  42.             $tmpname = tempnam("/tmp", "speakAIFF");
  43.             $aiffname = $tmpname . ".aiff";
  44.             rename($tmpname, $aiffname);
  45.             
  46.     //        
  47.             
  48.             //echo "Temp AIFF File: $aiffname<br />";
  49.         
  50.                 // Save the sound file as an AIFF Sound
  51.             
  52.             $result = exec("osascript -e 'say \"$text\" saving to (POSIX file \"$aiffname\") with waiting until completion' -e 'return 1'");
  53.     
  54.             if($result == 1) {
  55.                 //echo "Result of creating AIFF File: $result<br />";
  56.                 
  57.                 $tmpname = tempnam("/tmp", "speakWAV");
  58.                 $wavname = $tmpname . ".wav";
  59.                 rename($tmpname, $wavname);
  60.                 
  61.                     // Export the sound file to WAV Format
  62.                 //$result = exec("osascript -e 'beep'");
  63.                 $result = exec("osascript -e 'tell application \"QuickTime Player\" to set theFile to open alias (POSIX file \"$aiffname\" as text)' -e 'tell application \"QuickTime Player\" to export theFile to (POSIX file \"$wavname\") as wave with replacing' -e 'tell application \"QuickTime Player\" to close theFile' -e 'return 1'");
  64.                 //echo "Result of exporting to WAV: $result<br />";
  65.         
  66.                 if($result == 1) {
  67.                 
  68.                         // first delete the temp AIFF file
  69.                         
  70.                     @unlink($aiffname);
  71.                     
  72.                         // now, send the WAV file to the client device
  73.                     
  74.                     $filename = basename($wavname);
  75.                     
  76.                     header("Cache-Control: no-cache, must revalidate");
  77.                     header("Pragma: no-cache");
  78.                     header("Content-type: audio/wav");
  79.                     header("Content-Disposition: inline; filename=\"$filename\"");
  80.                     header("Content-length:".(string)(filesize($wavname)));
  81.                     header("Content-transfer-encoding: binary");
  82.                 
  83.                     $fp = fopen($wavname,'rb');
  84.                     while(!feof($fp)) print fread($fp, 4096); 
  85.                     fclose($fp);
  86.                                         
  87.                         // We're done!
  88.                         
  89.                     exit;
  90.                 }
  91.             } 
  92.             
  93.             die("An error occurred while creating or playing the sound files.");
  94.         }     
  95.     }
  96.     
  97.     return ScriptResponse($response);
  98. }
  99.  
  100.  
  101. ?>